var tinggi=getClientHeight();
var lebar=getClientWidth();

function getClientHeight(){
	var theHeight;

	if (window.innerHeight) {
		theHeight=window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {
		theHeight=document.documentElement.clientHeight;
	}
	else if (document.body) {
		theHeight=document.body.clientHeight;
	}
	
	return theHeight;
}

function getClientWidth(){
	var theWidth;
	if (window.innerWidth) 
		theWidth=window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth) 
		theWidth=document.documentElement.clientWidth;
	else if (document.body) 
		theWidth=document.body.clientWidth;

	return theWidth;
}


function cek_url(modul,title,par_title){
	//$('#content_na').html('');
	$('#panel_main').html('').addClass('loadingna');
	/*$('#panel_main').addClass('loadingna');
		$.ajax({
		type: 'POST',
			url : host+'backend/get_form/'+modul,
			success:function(resp) {
				$('#panel_main').removeClass('loadingna');
				$('#content_na').html(resp);
				
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
					
					alert('Modul Tidak Ada');
			}
		
		});	
		*/
		$.get(host+'backend/get_form/'+modul,function(resp){
			try{
				$('#panel_main').removeClass('loadingna').html(resp);
				//$('#content_na').html(resp);
			}
			catch(err){
				alert('Modul Tidak Ada');
			}
		});
}

function windowLoading(html,judul,width,height){
    divcontainerz = "win"+Math.floor(Math.random()*9999);
    $("<div id="+divcontainerz+"></div>").appendTo("body");
    divcontainerz = "#"+divcontainerz;
    $(divcontainerz).html(html);
    $(divcontainerz).css('padding','5px');
    $(divcontainerz).window({
       title:judul,
       width:width,
       height:height,
       autoOpen:false,
       modal:true,
       maximizable:false,
       resizable:false,
       minimizable:false,
       closable:false,
       collapsible:false,  
    });
    $(divcontainerz).window('open');        
}
function winLoadingClose(){
    $(divcontainerz).window('close');
    //$(divcontainer).html('');
}
function loadingna(){
	windowLoading("<img src='"+host+"assets/images/loading.gif' style='position: fixed;top: 50%;left: 50%;margin-top: -10px;margin-left: -25px;'/>","Data Sedang Diproses",200,100);
}


function add_koma(nStr) {
  nStr += '';
  x = nStr.split(',');
  x1 = x[0];
  x2 = x.length > 1 ? ',' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}

function mywindow(txt,judul,width,height){
    
    divcontainer = "mywindow"+Math.floor(Math.random()*99999);
    $("<div id='"+divcontainer+"'></div>").appendTo("body");
    divcontainer = "#"+divcontainer;
	 $(divcontainer).html("");
    $(divcontainer).css({'padding':'10px'});
    $(divcontainer).html(txt);

    if (judul === undefined ) judul ='DATA';
    if (width === undefined ) {
        var wdh = $(divcontainer).outerWidth(true);
        if ( wdh > 750 ) wdh = 750;
        if ( wdh < 250 ) wdh = 250;
        width = wdh ;
    }
    if (height === undefined ){
        var hgt = $(divcontainer).outerHeight(true);
        if ( hgt > 550 ) hgt = 550;
        if ( hgt < 150 ) hgt = 150;
        height = hgt ;
    }
    var leftCenter = window.innerWidth/2;
	var topCenter = window.innerHeight/2;
    var panelLeft = leftCenter - (width/2);
    var panelTop = topCenter - (height/2);
    $(divcontainer).css({'opacity':0});
    $(divcontainer).window({
                            title: judul,
                            autoOpen: false,
                            left:panelLeft,
                            top:panelTop,
                            width:width,
                            height:height,
                            modal: true,
                            shadow: false,
                            closed: true,
                            collapsible:true,
                            collapsed:false,
							minimizable:false,
							maximizable:false,
							collapsible:false,
							resizable:false,
                            onBeforeClose:function(){
								
								$($(divcontainer).window('panel')).animate({
									"width": "-15px",
									"height":"-15px",
									"left":  leftCenter +"px",
									"top": topCenter +"px",
									"opacity":0
								  }, 777,function(){
									  $(divcontainer).html('');
									 
										$($(divcontainer).window('panel')).css({'left':panelLeft+'px','top':panelTop+'px','width':width+'px','height':height+'px'});
										
									   $(divcontainer).window("close",true);
									    $(divcontainer).window("destroy",true);
									  return true;
									  });
								
							},
							onBeforeOpen:function(){
									
								$($(divcontainer).window('panel')).css({'left':leftCenter+'px','top':topCenter+'px','width':'1px','height':'1px'});
							},
							onOpen:function(){
								 $($(divcontainer).window('panel')).animate({
									"width": (width+10)+"px",
									"height":(height+45)+"px",
									"left": panelLeft+"px",
									"top": panelTop+"px",
									'opacity':1
								  }, 777,function(){
									  $(divcontainer).animate({'width':(width-13)+'px','height':(height)+'px','opacity':1}
									  ,777
									  ,function(){
										   //$(divcontainer).window('resize');
										  });
									  });
							}
							
                            });
  $(divcontainer).window('open');
   $(divcontainer).window('open');
}

function mywindow_close(){
	$(divcontainer).window('close');
}

function submitform (frm,func){
  
 var url = jQuery('#'+frm).attr("url");
    jQuery('#'+frm).form('submit',{
            url:url,
            onSubmit: function(){
                  return $(this).form('validate');
            },
            success:function(data){
                if (func == undefined ){
                     if (data == "1"){
                        pesan('Data Saved ','Success');
                    }else{
                         pesan(data,'Result');
                    }
                }else{
                    func(data);
                }
            },
            error:function(data){
                 if (func == undefined ){
                     pesan(data,'Error');
                }else{
                    func(data);
                }
            }
    });
}

function loadUrl(obj, urls){
	$('.btn-highlight').removeClass("btn-highlight");
	obj.className += " btn-highlight";	
	
    $("#mainContainer").html("").addClass("loading");
	$.get(urls,function (html){
	    $("#mainContainer").html(html).removeClass("loading");
    });
}

var myVar = setInterval(function(){
	d = new Date();
    t = d.toLocaleString();
	$('#waktu').html(d.toLocaleString());
},1000);

function formatDate(date) {
				var bulan=date.getMonth() +1;
				var tgl=date.getDate();
				if(bulan < 10){
					bulan='0'+bulan;
				}
				
				if(tgl < 10){
					tgl='0'+tgl;
				}
	        	return date.getFullYear() + "-" + bulan + "-" + tgl;
}	

function get_form(url,post,w,h,title){
	$.post(url,post,function(r){
		mywindow(r,title,w,h);	
	});
		
}

function fillCombo(url, SelID, value, value2, value3, value4){
	//if(Ext.get(SelID).innerHTML == "") return false;
	if (value == undefined) value = "";
	if (value2 == undefined) value2 = "";
	if (value3 == undefined) value3 = "";
	if (value4 == undefined) value4 = "";
	
	$.post(url, {"v": value, "v2": value2, "v3": value3, "v4": value4},function(data){
		$('#'+SelID).append(data);
	});

}

function add_koma(nStr) {
		nStr += '';
		x = nStr.split(',');
		x1 = x[0];
		x2 = x.length > 1 ? ',' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}

var url = window.URL || window.webkitURL;
var sts_error="";
var ukuran;

function cek_size(obj,w,h,ukur){
    if(obj.disabled ){
        alert('Your browser does not support File upload.');
    }else{
        var chosen = obj.files[0];
        var image = new Image();
        image.onload = function() {
			ukuran=Math.round(chosen.size/1024);
			console.log(ukuran+'>'+ukur+" lebar : "+this.width+" > " + w +" tinggi : "+this.height+">"+h);
			if(parseFloat(ukuran) > parseFloat(ukur)){
				$.messager.alert("File Gambar","Ukuran File Terlalu Besar",'error');	
				$('#'+obj.id).val('');
				return;	
			}
			else{
				 if(parseFloat(this.width) > parseFloat(w) && parseFloat(this.height) > parseFloat(h)){
					$.messager.alert("File Gambar","Ukuran File Tidak Sesuai",'error');
					$('#'+obj.id).val('');
					return;	
				}
			}
           
			
			//alert('Width:'+this.width +' Height:'+ this.height+' '+ Math.round(chosen.size/1024)+'KB');
			
        };
        image.onerror = function() {
            alert('Bukan File Image : '+ chosen.type);
        };
        image.src = url.createObjectURL(chosen);                    
     }

}

function formatDuit(num,cur) {
    var p = num.toFixed(2).split(".");
    return cur + p[0].split("").reverse().reduce(function(acc, num, i, orig) {
        return  num + (i && !(i % 3) ? "," : "") + acc;
    }, "") + "." + p[1];
}

function tampil(mod,sts,data){
	//alert ("a");
	//$.messager.alert("Simpan Data","Data Gagal Tersimpan",'error');
	$("<div id='xx' style='padding:5px;background:#fff;border:1px solid #ccc;'><div id='isi'></div></div>").appendTo("body");
	$('#isi').html('').addClass('loadingna');
	$.post(host+"frontend/get_form/daftar",{mod:mod,sts:sts,data:data},function(r){
		$('#isi').removeClass('loadingna').html(r);
	});
	
		   $('#xx').window({  
			 title: 'Registrasi Member Bikin Kaos Bebas',
             autoOpen: false,
             width:650,
             height:getClientHeight()-200,
             modal: true,
			 shadow: false,
             closed: true,
             collapsible:true,
             collapsed:false,
			 minimizable:false,
			 maximizable:false,
			 collapsible:false,
			 resizable:false,
		 });  
		 
		 $('#xx').window('open');
		 
	}
	
function thumb()
{
		$("#thumb1").click(function(){
			$("#image_produk1").fadeIn("5000");
			$("#image_produk2").fadeOut("8000");
			$("#image_produk3").fadeOut("8000");
		  });
		  
		  $("#thumb2").click(function(){
			$("#image_produk1").fadeOut("8000");
			$("#image_produk2").fadeIn("5000");
			$("#image_produk3").fadeOut("8000");
		  });
		  
		  $("#thumb3").click(function(){
			$("#image_produk1").fadeOut("8000");
			$("#image_produk2").fadeOut("8000");
			$("#image_produk3").fadeIn("5000");
		  });
		  
}

function showimagepopup($p1){
	 //alert($p1);
	 	$('#overlay').fadeIn('fast');
		//$('#fullimage').attr('src',host+'repositori/porto_folio/'+p1);
		$('#gambarto').attr('src',host+'repositori/porto_folio/'+$p1)
		$('#fullimage').css('display','block');
        $('#fullimage').animate({'left':'30%'},500);
}

function closeimagepopup(){
	 //alert($p1);
	 	$('#overlay').fadeOut('fast');
		//$('#fullimage').attr('src',host+'repositori/porto_folio/'+p1);
		//$('#gambarto').attr('src',host+'repositori/porto_folio/'+$p1)
		$('.boxing').css('display','block');
        $('#fullimage').animate({'left':'100%'},500);
		
}


function ganti_gambar(id_foto){
	$("#foto_na").fadeOut("2000");
	$('#foto_na').attr('src',host+'repositori/produk_user/'+id_foto)
	$("#foto_na").fadeIn("5000");
}

function get_dataproduk(id){
	$('#content').html('').addClass('loadingna');
	$.post(host+'frontend/get_form/daftar_produk/'+id,function(r){
		$('#content').removeClass('loadingna').html(r);
	});	
}

function pesan(txt,judul,icon){
     if (icon == undefined ) icon ='info';
    $.messager.alert(judul,txt,icon);
}